package com.woniu.microworkorderserver.workgroupmag.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import com.woniu.microcommonsserver.entity.UserInfoEntity;
import com.woniu.microcommonsserver.entity.WorkGroupInfoEntity;
import com.woniu.microcommonsserver.entity.WorkgroupUserInfoEntity;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microworkorderserver.param.UserResult;
import com.woniu.microworkorderserver.workgroupmag.mapper.WorkGroupMapper;
import com.woniu.microworkorderserver.workgroupmag.mapper.WorkGroupUserMapper;
import com.woniu.microworkorderserver.workgroupmag.service.WorkGroupUserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WorkGroupUserServiceImpl implements WorkGroupUserService {

    @Resource
    private WorkGroupUserMapper workGroupUserMapper;
    @Resource
    private WorkGroupMapper workGroupMapper;

    @Override
    public PageInfo<WorkgroupUserInfoEntity> findPage(PageRequest request) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit())
                .doSelectPageInfo(() -> workGroupUserMapper.findPage(request));
    }

    @Override
    public void delete(long workGroupId) {
        workGroupUserMapper.delete(workGroupId);
    }

    @Override
    public int[] findByGroupId(long workGroupId) {
        return workGroupUserMapper.findByGroupId(workGroupId);
    }

    @Override
    public List<UserResult> findByUserId(PageInfo pageInfo) {
        System.out.println(pageInfo);
        List<UserResult> results = new ArrayList<>();
        List<JSON> datas = new ArrayList<>();
        if(pageInfo != null){
            datas = pageInfo.getList();
        }
        if(datas != null && datas.size() > 0){
            datas.forEach(e -> {
                UserResult userResult = new UserResult();
                UserInfoEntity user = JSONObject.toJavaObject(e,UserInfoEntity.class);
                userResult.setId(user.getId());
                userResult.setUserName(user.getUserName());
                userResult.setLoginName(user.getLoginName());
                userResult.setTelNumber(user.getTelNumber());
                int[] ids = workGroupUserMapper.findByUserId(user.getId());
                String workGroup = "";
                for (int id : ids) {
                    WorkGroupInfoEntity workGroupInfoEntity = workGroupMapper.findById(id);
                    if(workGroupInfoEntity != null){
                        workGroup += workGroupInfoEntity.getWorkGroupName() + "/";
                    }
                }
                userResult.setWorkGroup(workGroup);
                results.add(userResult);
            });
        }
        return results;
    }
}
